package com.aibaixun.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.aibaixun.common.core.domain.R;
import com.aibaixun.common.utils.BeanCopyUtils;
import com.aibaixun.system.domain.dto.BindDeviceDto;
import com.aibaixun.system.domain.dto.DicmNvrPageDto;
import com.aibaixun.system.domain.vo.QueryDeviceTreeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aibaixun.system.domain.DicmNvr;
import com.aibaixun.system.service.DicmNvrService;
import com.aibaixun.system.mapper.DicmNvrMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author zhangyh
* @description 针对表【dicm_nvr(摄像头信息表)】的数据库操作Service实现
* @createDate 2023-09-14 16:21:23
*/
@Service
public class DicmNvrServiceImpl extends ServiceImpl<DicmNvrMapper, DicmNvr>
    implements DicmNvrService{

    @Override
    public R<Page<DicmNvr>> getPage(Page page, DicmNvrPageDto pageDto) {
        DicmNvr dicmNvr = new DicmNvr();
        BeanCopyUtils.copy(pageDto,dicmNvr);
        LambdaQueryWrapper<DicmNvr> queryWrapper = new LambdaQueryWrapper<>(dicmNvr);
        Page<DicmNvr> resultPage = this.baseMapper.selectPage(page,queryWrapper);
        for (DicmNvr record : resultPage.getRecords()) {
            record.setRtspUrl("rtsp://"+record.getUserName()+":"+record.getUserPassword()
                +"@"+record.getNvrIp()+":554/Streaming/Channels/"+record.getChanelNo()+"01");
        }
        return R.ok(resultPage);
    }

    @Override
    public List<DicmNvr> getAllDeviceUe() {
        return this.baseMapper.getAllDeviceUe();
    }

    @Override
    public R<DicmNvr> getNvrDetail(String nvrId) {
        DicmNvr dicmNvr = this.baseMapper.selectById(nvrId);
        if(dicmNvr == null){
            return R.fail("设备信息不存在！");
        }
        dicmNvr.setRtspUrl("rtsp://"+dicmNvr.getUserName()+":"+dicmNvr.getUserPassword()
            +"@"+dicmNvr.getNvrIp()+":554/Streaming/Channels/"+dicmNvr.getChanelNo()+"01");
        return R.ok(dicmNvr);
    }

    @Override
    public String getNvrDetailUrl(String nvrId) {
        DicmNvr dicmNvr = this.baseMapper.selectById(nvrId);
        String nvrUrl = null;
        if(dicmNvr == null){
            return nvrUrl;
        }
        nvrUrl ="rtsp://"+dicmNvr.getUserName()+":"+dicmNvr.getUserPassword()
            +"@"+dicmNvr.getNvrIp()+":554/Streaming/Channels/"+dicmNvr.getChanelNo()+"01";
        return nvrUrl;
    }

    @Override
    public void updatenvrUeId(BindDeviceDto dto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("address_id",dto.getId());
        List<DicmNvr> nvrList = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(nvrList)){
            nvrList.forEach(item -> {
                if (StringUtils.isNotEmpty(item.getUeId())){
                    String ueId = "";
                    String[] split = item.getUeId().split("-");
                    for (int i = 0; i < split.length; i++) {
                        if (i < 3){
                            ueId = ueId+split[i]+"-";
                        }else if (i == 3){
                            ueId = ueId+split[i];
                        }
                    }
                    String newUeId = item.getUeId().replace(ueId, dto.getUeId());
                    item.setUeId(newUeId);

//                    String patternString = "^(\\d+)-(\\d+)-(\\d+)-(\\d+)";
//                    Pattern pattern = Pattern.compile(patternString);
//                    Matcher matcher = pattern.matcher(item.getUeId());
//                    if (matcher.find()) {
//                        String oldUeId = matcher.group();
//                        String ueId = item.getUeId();
//                        String newUeId = ueId.replace(oldUeId, dto.getUeId());
//                        item.setUeId(newUeId);
//                    }
                }else {
                    item.setUeId(dto.getUeId()+"-"+item.getNvrName());
                }

                baseMapper.updateById(item);
            });
        }
    }

    @Override
    public void resetUeId() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ue_id","空");
        List<DicmNvr> nvrList = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(nvrList)){
            nvrList.forEach(item -> {
                item.setUeId("");
                baseMapper.updateById(item);
            });
        }
    }

    @Override
    public void addNvrMessage(List<DicmNvr> nvrList) {

    }

    @Override
    public List<DicmNvr> queryNvrList() {
        return baseMapper.queryNvrList();
    }

    @Override
    public void updateNvrById(List<DicmNvr> dicmNvrList) {
        if (CollectionUtil.isNotEmpty(dicmNvrList)){
            dicmNvrList.forEach(item -> {
                baseMapper.updateById(item);
            });
        }
    }

    @Override
    public boolean saveDicnNvrList(List<DicmNvr> nvrList) {
        List<DicmNvr> saveList =new ArrayList<>();
        if (CollectionUtil.isNotEmpty(nvrList)){
            nvrList.forEach(item -> {
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("address_id",item.getAddressId());
                DicmNvr dicmNvr = baseMapper.selectOne(queryWrapper);
                if (null == dicmNvr){
                    saveList.add(item);
                }
            });
        }
        if (CollectionUtil.isNotEmpty(saveList)){
            boolean b = this.saveBatch(saveList);
            return b;
        }
        return false;
    }

    @Override
    public List<QueryDeviceTreeVo> queryNvrTreeList(String parentId) {
        return baseMapper.queryNvrTreeList(parentId);
    }

    @Override
    public boolean updateNvr(BindDeviceDto dto) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("nvr_id",dto.getId());
        updateWrapper.set("ue_id",dto.getUeId());
        int update = baseMapper.update(null, updateWrapper);
        if (update != 0){
            return true;
        }
        return false;
    }

    @Override
    public List<String> queryDeviceId() {
        return baseMapper.queryDeviceId();
    }

    @Override
    public List<String> queryNvrDeviceId() {
        return baseMapper.queryNvrDeviceId();
    }
}




